package com.yuju.controller.accounts;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.account.enums.AccountStatusEnum;
import com.yuju.account.enums.AccountTypeEnum;
import com.yuju.account.enums.BusinessTypeDetailEnum;
import com.yuju.account.enums.BusinessTypeEnum;
import com.yuju.account.enums.ReserveAccountReturnCodeEnum;
import com.yuju.model.account.AccountModel;
import com.yuju.util.Utils;
import com.yuju.account.service.IAccountService;
import com.yuju.common.Result;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.vo.account.AccountVO;

/**
 * 类名：ReserveAccountController  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2017年12月4日 下午1:48:44  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("reserveAccount")
public class ReserveAccountController extends BaseController<AccountVO, AccountModel> {
	
	@Autowired
	private IAccountService accountService;
	
	/**
	 * 功能：预订单账务查询<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月19日 下午4:53:14 <br/>
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("listByReserveId")
	@ResponseBody
	public Result listByReserveId(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		Map<String, Object> map = accountService.listByReserveId(convert(accountVO));
		List<AccountModel> accountList = (List<AccountModel>) map.get("accountList");
		Map<String, Object> sumMap = (Map<String, Object>) map.get("sumMap");
		Result result = new Result();
		if(Utils.isNotNull(accountList) && Utils.isNotNull(sumMap)){
			result.setData(sumMap);
			result.setRows(accountList);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_QUERY_SUCCESS.getCode());
			result.setReturnMsg(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_QUERY_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_QUERY_FAILURE.getCode());
			result.setReturnMsg(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_QUERY_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：新增收订金<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月4日 上午10:53:23 <br/>
	 */
	@RequestMapping("saveDeposit")
	@ResponseBody
	public Result saveDeposit(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		Result result = new Result();
		accountVO.setAmount(1);
		accountVO.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
		accountVO.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
		accountVO.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
		accountVO.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_DJ.getValue());
		accountVO.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_DJ.getName());
		accountVO.setBusinessDetail(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail())+BusinessTypeEnum.BUSINESS_TYPE_DJ.getName());
		accountVO.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail()));
		accountVO.setCreateUserId(accountVO.getEmpId());
		String i = accountService.saveReserveAccount(convert(accountVO));
		if(Utils.isNotNull(i)){
			result.setData(i);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_SAVE_SUCCESS.getCode());
			result.setReturnMsg(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_SAVE_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_SAVE_FAILURE.getCode());
			result.setReturnMsg(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：新增退款<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月4日 下午2:15:00 <br/>
	 */
	@RequestMapping("saveBackDeposit")
	@ResponseBody
	public Result saveBackDeposit(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		Result result = new Result();
		accountVO.setAmount(1);
		accountVO.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
		accountVO.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
		accountVO.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
		accountVO.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_TK.getValue());
		accountVO.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_TK.getName());
		accountVO.setBusinessDetail(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail())+BusinessTypeEnum.BUSINESS_TYPE_TK.getName());
		accountVO.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountVO.getBusinessTypeDetail()));
		accountVO.setCreateUserId(accountVO.getEmpId());
		String i = accountService.saveReserveAccount(convert(accountVO));
		if(Utils.isNotNull(i)){
			result.setData(i);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_BACK_SUCCESS.getCode());
			result.setReturnMsg(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_BACK_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_BACK_FAILURE.getCode());
			result.setReturnMsg(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_BACK_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：新增冲调<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月4日 下午2:38:02 <br/>
	 */
	@RequestMapping("saveReduce")
	@ResponseBody
	public Result saveReduce(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		Result result = new Result();
		
		//更新旧账务
		accountVO.setAccountStatus(AccountStatusEnum.STATUS_CANCEL.getValue());
		accountVO.setAccountStatusName(AccountStatusEnum.STATUS_CANCEL.getName());
		accountVO.setUpdateUserId(accountVO.getEmpId());
		accountVO.setGmtModified(new Date());
		
		String i = accountService.saveReduce(convert(accountVO));
		
		if(Utils.isNotNull(i)){
			result.setData(i);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_REDUCE_SUCCESS.getCode());
			result.setReturnMsg(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_REDUCE_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_REDUCE_FAILURE.getCode());
			result.setReturnMsg(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_REDUCE_FAILURE.getDesc());
		}
		return result;
	}
}
